package com.neusoft.hifly.rpc.area.service.impl;

import com.neusoft.hifly.core.log.Log;
import com.neusoft.hifly.core.pojo.ApiCode;
import com.neusoft.hifly.core.pojo.ApiResponse;
import com.neusoft.hifly.rpc.area.dao.IProvDao;
import com.neusoft.hifly.rpc.area.entity.ProvObj;
import com.neusoft.hifly.rpc.area.service.IProvService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Title: 数据字典服务
 * <p>
 * Description: 省份信息 Service
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lihui
 * @version 1.0.0
 */
@Service
public class ProvServiceImpl  implements IProvService {
	@Autowired(required = false)
	private IProvDao provDao;


	/**
	 * 根据id查询对象
	 *
	 * @param logicId
	 *            对象logicId
	 * @return 对象
	 */
	@Override
	public ProvObj getByLogicId(final long logicId) {
		ApiResponse<?> result=provDao.getByLogicId(logicId);
		if (result.getCode() == ApiCode.ERROR.getCode()) {
			Log.error(result.getMessage());
			return null;
		}
		ObjectMapper mapper = new ObjectMapper();
		ProvObj provobj = mapper.convertValue(result.getData(), ProvObj.class);
		return provobj;
	}

	/**
	 * 查询所有对象
	 *
	 * @return 对象集合
	 */
	@Override
	@Cacheable(value = "dic:prov")
	public List<ProvObj> findAll() {
		final ApiResponse<List<ProvObj>> result =  provDao.findAll();

		if (result.getCode() == ApiCode.ERROR.getCode()) {
			Log.error(result.getMessage());
			return null;
		}
		return result.getData();

	}
}
